package com.jeomo.mem.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.constant.SearchParam;
import com.jeomo.common.core.constant.Sort;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.masterdata.starter.service.ILevelService;
import com.jeomo.mem.starter.entity.MemberPointsActivityRule;
import com.jeomo.mem.starter.mapper.MemberPointsActivityMapper;
import com.jeomo.mem.starter.mapper.MemberPointsActivityRuleMapper;
import com.jeomo.mem.starter.service.IMemPointsActivityInfoService;
import com.jeomo.mem.starter.service.IMemPointsActivityService;
import com.jeomo.mem.starter.util.MemberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:Jackpot
 * @create 2020-09-21 17:21
 * @description:
 */
@Service
@Transactional
public class MemberPointsActivityServiceImpl extends BaseServiceImpl<MemberPointsActivityRuleMapper, MemberPointsActivityRule>
        implements IMemPointsActivityService {

    private final String USE_LEVEL = "use_level";
    private final String USE_LEVELS = "use_levels";
    private final String USE_RANGE = "use_range";
    private final String ACTIVITY_CODE = "activity_code";
    private final String START_FLAG = "start_flag";

    private final String CREATE_TIME = "create_time";

    @Resource
    private MemberPointsActivityMapper memPointsActivityMapper;
    @Resource
    private ILevelService levelService;
    @Resource
    private IMemPointsActivityInfoService memPointsActivityInfoService;

    @Override
    public PageResponseDto pageSearch(PageQueryDto dt) {
        Map<String, Object> searchParams = dt.getSearchParams();
        if(searchParams!=null){
            Map<String, Object> params = new HashMap<>(8);
            params.put(SearchParam.SEARCH_EQ+ACTIVITY_CODE,searchParams.get(ACTIVITY_CODE));
            params.put(SearchParam.SEARCH_EQ+USE_RANGE,searchParams.get(USE_RANGE));
            params.put(SearchParam.SEARCH_LIKE+USE_LEVELS,searchParams.get(USE_LEVEL));
            params.put(SearchParam.SEARCH_EQ+ START_FLAG,searchParams.get(START_FLAG));
            dt.setSearchParams(params);
        }
        Map<String, String> sort = new HashMap<>(8);
        sort.put(CREATE_TIME, Sort.DESC);
        dt.setSorts(sort);
        //处理查询结果
        PageResponseDto pageResponseDto = super.pageSearch(dt);
        Map<String, String> levelMap = levelService.queryCache();
        Map<Object, Object> activityMap = memPointsActivityInfoService.queryCache();
        List<?> items = pageResponseDto.getItems();
        for (Object item : items) {
            MemberPointsActivityRule memberPointsRule = (MemberPointsActivityRule)item;
            MemberUtil.assignName(memberPointsRule,levelMap);
            memberPointsRule.setActivityName(activityMap.get(memberPointsRule.getActivityCode()).toString());
        }
        return pageResponseDto;
    }

    @Override
    public boolean updateRuleStatus(String ruleCode,boolean status){

        MemberPointsActivityRule memberPointsActivityRule = new MemberPointsActivityRule();
        memberPointsActivityRule.setCode(ruleCode);
        memberPointsActivityRule.setStartFlag(status);
        return updateById(memberPointsActivityRule);
    }

}
